<template>
	<view>
		<return-search type="skyBlueReturn" placeholder="搜索我的吐槽" @searchResult="searchChange"></return-search>
		<!-- 我的社区 -->
		<scroll-view scroll-y enhanced refresher-enabled @scrolltolower="groundingHandle"
			@refresherrefresh="refresherrefreshHandle" :refresher-triggered="pullDownState" :show-scrollbar="false"
			lower-threshold="1000" class="scrollViewBox" :style="{height:contentHeight + 'px'}">
			<view v-if="communityList.length !== 0 && !searchState && !pullDownState" class="communityBox">
				<view class="communityItem" v-for="item in communityList" :key="item.intactArticleId">
					<public-comment :commentObject="item" :userShow="true" @deleteArticle="commentHandle"
						@clickId="disposeId">
					</public-comment>
				</view>
			</view>
			<scroll-loading :pullDownState="pullDownState" :requestState="requestState" :pagingState="pagingState"
				:dataList="communityList" :pageNumber="communityPageNumber" :totalPageNumber="communityTotalPageNumber"
				:searchDataState="searchDataState" :search="true" text="暂无文章" searchText="没有找到该文章"></scroll-loading>
		</scroll-view>
	</view>
</template>

<script>
	import {
		capsuleTopHeight
	} from "../../mixins/mixins.js"
	export default {
		mixins: [capsuleTopHeight],
		data() {
			return {
				// 下拉内容框高度
				contentHeight: '',
				// 当前刷新状态
				pullDownState: false,
				// 请求节流阀
				requestState: false,
				// 搜索状态
				searchState: false,
				// 分页加载状态
				pagingState: false,
				// 是否为搜索的数据
				searchDataState: false,
				// 搜索字
				queryData: null,

				// 社区当前请求的页数
				communityPageNumber: 1,
				// 社区总分页数
				communityTotalPageNumber: 0,
				// 社区总数据
				communityTotalCount: 0,
				// 社区每页请求的数量
				communityPageSize: 0,
				// 社区列表
				communityList: []
			};
		},
		created() {
			const wxWindowHeight = uni.getSystemInfoSync().windowHeight
			this.contentHeight = wxWindowHeight - this.capsuleHeight - this.capsuleTop - 10
			this.searchState = true
			this.getMyCommunityList()
		},
		methods: {
			// 用户点赞操作
			disposeId(id) {
				const result = this.communityList.find(
					(item) => item.intactArticleId === id
				);
				if (result.likeStatus == true) {
					result.likeStatus = false;
					if (result.likeStatus == false) {
						result.article.likeNum -= 1;
					}
				} else {
					result.likeStatus = true;
					if (result.likeStatus == true) {
						result.article.likeNum += 1;
					}
				}
			},
			// 获取社区数据
			async getMyCommunityList() {
				this.requestState = true
				const {
					data: result
				} = await uni.$http.get("/ym_server/IntactArticle/myArticle?page=" + this.communityPageNumber)
				if (result.code === 200 && result.data.list) {
					this.communityTotalPageNumber = result.data.list.pages
					this.communityTotalCount = result.data.list.total
					this.communityPageSize = result.data.list.size
					this.communityList = [...this.communityList, ...result.data.list.records]
				} else {
					this.communityPageNumber--
				}
				this.pullDownState = false
				this.requestState = false
				this.pagingState = false
				this.searchState = false
				this.searchDataState = false
			},
			// 分页加载
			groundingHandle() {
				if (this.requestState) return
				if (this.communityPageNumber < this.communityTotalPageNumber) {
					this.pagingState = true
					this.communityPageNumber++
					this.getMyCommunityList()
				}
			},
			// 社区下拉刷新
			async refresherrefreshHandle(type) {
				if (this.requestStatus || this.pullDownState) return
				this.pullDownState = true
				this.requestState = true
				this.searchState = false
				this.pagingState = false
				this.communityPageNumber = 1
				this.communityTotalPageNumber = 0
				this.communityPageSize = 0
				this.communityTotalCount = 0
				this.communityList = []
				if (this.searchDataState) {
					const {
						data: result
					} = await uni.$http.get(
						`/ym_server/IntactArticle/oneselfAllSearch?str=${this.queryData}&page=${this.communityPageNumber}`
					)
					if (result.code === 200) {
						if (result.data.list) {
							this.communityPageSize = result.data.list.size
							this.communityTotalPageNumber = result.data.list.pages
							this.communityTotalCount = result.data.list.total
							this.communityList = [...result.data.list.records]
						}
					}
					this.requestState = false
					this.searchState = false
					this.searchDataState = true
					this.pullDownState = false
				} else {
					this.getMyCommunityList()
				}
			},
			// 删除发布社区
			commentHandle(id) {
				console.log(id)
				wx.showModal({
					title: '是否删除该文章？',
					success: async (res) => {
						if (res.confirm) {
							const {
								data: result
							} = await uni.$http.delete("ym_server/IntactArticle/" + id)
							if (result.code === 200) {
								uni.showToast({
									title: "删除成功",
									icon: "none"
								})
								const index = this.communityList.findIndex(item => item.intactArticleId == id)
								this.communityList.splice(index, 1)
								this.communityTotalCount = this.communityTotalCount - 1
								this.communityTotalPageNumber = Math.ceil(this.communityTotalCount / this
									.communityPageSize)
							} else {
								uni.showToast({
									title: "删除失败",
									icon: "none"
								})
							}
						}
					}
				})
			},
			// 根据参数模糊查询
			async searchChange(query) {
				this.queryData = query
				this.communityPageNumber = 1
				this.communityTotalPageNumber = 0
				this.communityPageSize = 0
				this.communityTotalCount = 0
				this.communityList = []
				this.pullDownState = false
				this.requestState = false
				this.pagingState = false
				if (query === '') {
					this.searchState = true
					this.getMyCommunityList()
				} else {
					this.requestState = true
					this.searchState = true
					const {
						data: result
					} = await uni.$http.get(
						`/ym_server/IntactArticle/oneselfAllSearch?str=${query}&page=${this.communityPageNumber}`
					)
					if (result.code === 200) {
						if (result.data.list) {
							this.communityPageSize = result.data.list.size
							this.communityTotalPageNumber = result.data.list.pages
							this.communityTotalCount = result.data.list.total
							this.communityList = [...result.data.list.records]
						}
					}
					this.requestState = false
					this.searchState = false
					this.searchDataState = true
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scrollViewBox {
		width: 696upx;
		margin: 10px auto 0upx;

		.communityBox {
			width: 100%;

			.communityItem {
				width: 100%;

				&:last-child {
					margin-bottom: 32upx;
				}
			}
		}
	}
</style>
